/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telsoft.cms_admin.utility;

import java.util.List;
import java.util.Vector;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import telsoft.cms_admin.data.DATA_FormSystem;
import telsoft.cms_admin.entity.ETT_FormSystem;
import telsoft.cms_admin.util.BaseController;
import telsoft.util.StringUtil;

/**
 *
 * @author hoannc
 */
@ManagedBean(name = "system")
@ViewScoped
//@SuppressWarnings("unchecked")
public class FUNC_FormSystem extends BaseController {

    private boolean checkClearField = true;
    private ETT_FormSystem selectedRow;
    private ETT_FormSystem ETTFormSystem;
    private ETT_FormSystem ETTFormSystem_old;
    private List<ETT_FormSystem> listData;
    private DATA_FormSystem Model;

    public FUNC_FormSystem() {
        super();
        try {
            init();
        } catch (Exception ex) {
            addErrorMessage(ex);
            ex.printStackTrace();
        }

    }

    public boolean isCheckClearField() {
        return checkClearField;
    }

    public void setCheckClearField(boolean checkClearField) {
        this.checkClearField = checkClearField;
    }
    
    private void init() throws Exception {
        try {
            ETTFormSystem = new ETT_FormSystem();
            ETTFormSystem_old = new ETT_FormSystem();
            search();
        } catch (Exception ex) {
            addErrorMessage(ex);
            ex.printStackTrace();
        }
    }

    public void changeState(int iAction, boolean bNormalState) {
        if (bNormalState) {
            bRendered = false;
            bCancel = true;
            bSave = true;
        } else {
            bRendered = true;
            bCancel = false;
            bSave = false;
        }
        onChangeAction(iAction);
        setFieldEnable(bNormalState);
    }

    private void add() {
        try {
            if (validateInput()) {
                Model = new DATA_FormSystem();
                if (iAction == ACTION_ADDCOPY) {
                    Vector vtRow = new Vector();
                    ETTFormSystem.setCHAR_MODE(vtRow);
                    ETTFormSystem.setCONTENT_MODE(vtRow);
                }
                Model.fillParameter(ETTFormSystem);
                String strSys_ID = Model.insert();
                ETTFormSystem.setSYS_ID(strSys_ID);
                listData.add(ETTFormSystem);

                //set selected row
                selectedRow = ETTFormSystem;
            } else {
                fillDetailValue();
//                bFieldEnable = false;
//                setFieldEnable(true);
//                bRendered = false;
//                bCancel = true;
//                bSave = true;
            }
        } catch (Exception ex) {
            addErrorMessage(ex);
            fillDetailValue();
            ex.printStackTrace();
        }
    }

    private void search() {
        try {
            Model = new DATA_FormSystem();
            Model.fillParameter(ETTFormSystem);
            listData = Model.search();

            //Update UI
            if (listData.isEmpty()) {
                bDisableModifyRemove = true;
            } else {
                selectedRow = listData.get(0);
                ETTFormSystem = selectedRow;
            }
        } catch (Exception ex) {
            addErrorMessage(ex);
            fillDetailValue();
            ex.printStackTrace();
        }
    }

    private void remove() {
        try {
            Model = new DATA_FormSystem();
            if (selectedRow.getCHAR_MODE().size() > 0 || selectedRow.getCONTENT_MODE().size() > 0) {
                addErrorMessage("Không thể xóa, tồn tại dữ liệu liên quan!");
            } else {
                Model.fillParameter(selectedRow);
                Model.delete();
                //Update user interface
                listData.remove(selectedRow);

                //set selected row
                if (listData.isEmpty()) {
                    bDisableModifyRemove = true;
                } else {
                    selectedRow = listData.get(0);
                    ETTFormSystem = selectedRow;
                }
            }
        } catch (Exception ex) {
            addErrorMessage(ex);
            fillDetailValue();
            ex.printStackTrace();
        }
    }

    private void modify() {
        try {
            if (validateInput()) {
                Model = new DATA_FormSystem();
                Model.fillParameter(selectedRow);
                Model.remove();
                ///
                ETTFormSystem.setSYS_ID(selectedRow.getSYS_ID());
                Model = new DATA_FormSystem();
                Model.fillParameter(ETTFormSystem);
                Model.update();

                //Update user interface
                int i = 0;
                for (ETT_FormSystem item : listData) {
                    if (item.getSYS_ID().equals(ETTFormSystem.getSYS_ID())) {
                        listData.set(i, ETTFormSystem);
                        selectedRow = ETTFormSystem;
                        break;
                    }
                    i++;
                }
            } else {
                fillDetailValue();
            }
        } catch (Exception ex) {
            addErrorMessage(ex);
            fillDetailValue();
            ex.printStackTrace();
        }
    }

    public void onRowSelect() {
        try {
            bDisableModifyRemove = true;
            ETTFormSystem = (ETT_FormSystem) selectedRow.clone();
        } catch (Exception ex) {
            addErrorMessage(ex);
            ex.printStackTrace();
        }
    }

    public void onChangeAction(int iOld_Action) {
        if (iOld_Action == ACTION_ADD || iOld_Action == ACTION_SEARCH
                || iOld_Action == ACTION_REMOVE || iOld_Action == ACTION_MODIFY || iOld_Action == ACTION_ADDCOPY) {
            if (iOld_Action == ACTION_ADD) {
                iAction = ACTION_ADD;
                if(checkClearField)clearFieldValue();
            } else if (iOld_Action == ACTION_MODIFY) {
                iAction = ACTION_MODIFY;
                clearFieldValue();
                fillDetailValue();
            } else if (iOld_Action == ACTION_ADDCOPY) {
                iAction = ACTION_ADDCOPY;
                clearFieldValue();
                fillDetailValue();
            } else if (iOld_Action == ACTION_SEARCH) {
                iAction = ACTION_SEARCH;
                clearFieldValue();
            } else if (iOld_Action == ACTION_REMOVE) {
                iAction = ACTION_REMOVE;
                clearFieldValue();
                doAction();
            }
        } else if (iOld_Action == ACTION_SAVE || iOld_Action == ACTION_CANCEL) {
            if (iOld_Action == ACTION_SAVE) {
                doAction();
            } else if (iOld_Action == ACTION_CANCEL) {
                fillDetailValue();
            }
        }
    }

    public void fillDetailValue() {
        try {
            ETTFormSystem = (ETT_FormSystem) ETTFormSystem_old.clone();
        } catch (Exception ex) {
            addErrorMessage(ex);
        }
    }

    public void clearFieldValue() {
        try {
            ETTFormSystem_old = (ETT_FormSystem) ETTFormSystem.clone();
            ETTFormSystem = new ETT_FormSystem();
        } catch (Exception ex) {
            addErrorMessage(ex);
        }
    }

    public void doAction() {
        switch (iAction) {
            case ACTION_ADD:
                add();
                break;
            case ACTION_ADDCOPY:
                add();
                break;
            case ACTION_MODIFY:
                modify();
                break;
            case ACTION_SEARCH:
                search();
                break;
            case ACTION_REMOVE:
                remove();
                break;
        }
    }

    public boolean validateInput() {
        if (StringUtil.nvl(ETTFormSystem.getSYS_CODE(), "").equals("")) {
            addErrorMessage("Trường 'Mã hệ thống' không được để trống!");
            checkClearField = false;
            return false;
        }
        if (StringUtil.nvl(ETTFormSystem.getSYS_NAME(), "").equals("")) {
            addErrorMessage("Trường 'Tên hệ thống' không được để trống!");
            checkClearField = false;
            return false;
        }
        if (StringUtil.nvl(ETTFormSystem.getSTATUS(), "").equals("")) {
            addErrorMessage("Trường 'Trạng thái' phải được chọn!");
            checkClearField = false;
            return false;
        }
        checkClearField = true;
        return true;
    }

    public void setFieldEnable(boolean b) {
        if (b) {
            bFieldEnable = false;
        } else {
            bFieldEnable = true;
        }
    }

    public ETT_FormSystem getSelectedRow() {
        return selectedRow;
    }

    public List<ETT_FormSystem> getListData() {
        return listData;
    }

    public void setSelectedRow(ETT_FormSystem selectedRow) {
        this.selectedRow = selectedRow;
    }

    public void setListData(List<ETT_FormSystem> listData) {
        this.listData = listData;
    }

    public ETT_FormSystem getETTFormSystem() {
        return ETTFormSystem;
    }

    public void setETTFormSystem(ETT_FormSystem ETTFormSystem) {
        this.ETTFormSystem = ETTFormSystem;
    }
}
